package com.qzyh.esb.egsa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzyh.esb.egsa.bean.vo.MenuTreeVO;
import com.qzyh.esb.egsa.bean.vo.UserMenuTreeVO;
import com.qzyh.esb.egsa.entity.Menu;
import com.qzyh.esb.egsa.entity.Role;
import com.qzyh.esb.egsa.entity.RoleMenu;
import com.qzyh.esb.egsa.entity.UserRole;
import com.qzyh.esb.egsa.mapper.MenuMapper;
import com.qzyh.esb.egsa.mapper.RoleMenuMapper;
import com.qzyh.esb.egsa.mapper.UserRoleMapper;
import com.qzyh.esb.egsa.service.IMenuService;
import com.qzyh.esb.egsa.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author whq
 * @since 2023-03-17
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    private static final Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<MenuTreeVO> getList(String menuName) {
        QueryWrapper<Menu> wrapper = Wrappers.query();
        if (StringUtils.hasLength(menuName)){
            wrapper.eq("menu_name", menuName);
        }
        wrapper.orderByAsc("menu_sort+1");
        List<Menu> menuList = menuMapper.selectList(wrapper);
        List<MenuTreeVO> menuTreeVOList = MenuConverter.INSTANCE.menuListToMenuTreeVOList(menuList);
        return TreeBuilder.buildTree(menuTreeVOList);
    }

    @Override
    @Transactional
    public ResultObject deleteById(String id) {
        //判断该id是否为父级菜单，父级菜单不允许直接删除
        QueryWrapper<Menu> wrapper = Wrappers.query();
        wrapper.eq("parent_id", id);
        List<Menu> menuList = menuMapper.selectList(wrapper);
        if (menuList!=null && menuList.size()>0){//查询数据不为空
            return ResultObject.fail("该菜单为父级菜单，不允许直接删除，请先删除子菜单");
        }
        menuMapper.deleteById(id);
        //删除角色菜单关联表
        QueryWrapper<RoleMenu> roleMenuQueryWrapper = Wrappers.query();
        roleMenuQueryWrapper.eq("menu_id", id);
        roleMenuMapper.delete(roleMenuQueryWrapper);
        return ResultObject.success("删除成功");
    }

    @Override
    public List<String> getFatherTree(List<MenuTreeVO> list, String id){
        return TreeBuilder.getFatherTree(list,id);
    }

    @Override
    public List<UserMenuTreeVO> queryByRoleId(String roleId) {
        String userId = BaseContextUtil.getUserId();
        QueryWrapper<UserRole> userRoleQueryWrapper = Wrappers.query();
        userRoleQueryWrapper.eq("user_id", userId);
        userRoleQueryWrapper.eq("role_id", roleId);
        List<UserRole> userRoleList = userRoleMapper.selectList(userRoleQueryWrapper);
        //有且只有一条 用户与角色的对应关系 才算校验通过。
        if (userRoleList != null && userRoleList.size() ==1){
            //获取菜单权限集合
            List<Menu> menuList = menuMapper.selectListByRoleId(roleId);
            //转化成菜单的数据格式
            List<UserMenuTreeVO> userMenuTreeVOList = UserMenuConverter.INSTANCE.menuListToUserMenuTreeVOList(menuList);
            //转化成树形结构
            return TreeBuilder.buildTree(userMenuTreeVOList);
        }
        return null;
    }
}
